// pages/sign/sign.js
import { sign, signTotal } from '../../api/tlSign'
import { getTbPersonals } from '../../api/tbPersonal'
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
var qdflag = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude: 113.324520,
    latitude: 23.099994,
    company: "南京联创北斗",
    scale: 14,
    time: "",
    date: "",
    address: "",
    qdtimes: '' || 0,
    isqiandao: "签到",
    code: "",
  },


  qiandaoclick() {
    var that = this
    let data = {
      "lat": that.data.latitude,
      "lon": that.data.longitude,
      "personalCode": that.data.code,
      "param1": that.data.address
    }
    sign(data).then(res => {
      that.setData({
        isqiandao: "已签到"
      })

    })
    let data1 = {
      "personalCode": that.data.code,
      "signTime": that.data.datechuan
    }
    signTotal(data1).then(res => {
      console.log(res)
      that.setData({
        qdtimes: res
      })

    })

  },







  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (wx.getStorageSync('personalCode')) {
      this.setData({
        code: wx.getStorageSync('personalCode')
      })
    }





    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    var h = now.getHours();
    var m = now.getMinutes();
    if (h < 10) {
      h = '0' + h;
    };
    if (m < 10) {
      m = '0' + m;
    };
    this.setData({
      date: year + '年' + month + '月' + day + '日',
      datechuan: year + month + day,
      time: h + ":" + m
    })


    var _this = this;
    let data = {
      "telephone": wx.getStorageSync('phone')
    }
    getTbPersonals(data).then(res => {
      _this.setData({
        company: res[0].companyName,
        code: res[0].code
      })
    })

    let data1 = {
      "personalCode": _this.data.code,
      "signTime": _this.data.datechuan
    }
    signTotal(data1).then(res => {
      _this.setData({
        qdtimes: res || 0
      })
    })







    qqmapsdk = new QQMapWX({
      key: '7AHBZ-WSSK3-3IB32-YR6ZX-2MMNE-LUBVJ'
    });





    wx.getLocation({
      success: res => {

        _this.setData({
          longitude: res.longitude,
          latitude: res.latitude
        })

        qqmapsdk.reverseGeocoder({
          location: {
            longitude: res.longitude,
            latitude: res.latitude
          },
          get_poi: 1,
          poi_options: 'policy=2;radius=3000;page_size=20;page_index=1',
          success: function (addres) {
            _this.setData({
              address: addres.result.address
            })

          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });

      }
    })
  },

  goStats() {
    wx.navigateTo({
      url: '../../pages/signstats/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})